package com.lsyw.intergrate.common;

import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author 刘磊
 * @version 1.0
 * @date 2020/8/4 11:03
 */
@Component
@PropertySource("classpath:application.yml")
public class FileDfsUtils {


    /**
     * @Description: 根据文件路径下载文件
     * @param filePath 文件路径
     * @return 文件字节数据
     * @throws Exception byte[]
     */
    /*public byte[] downFile(String filePath) {
        StorePath storePath = StorePath.parseFromUrl(filePath);

        return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadCallback<byte[]>() {
            @Override
            public byte[] recv(InputStream ins) throws IOException {
                return IOUtils.toByteArray(ins);
            }
        });
    }*/

    /**
     * 112发布
     *
     * @param filePath 文件路径
     * @return 文件字节数据
     * @throws Exception byte[]
     * @Description: 根据文件路径下载文件
     */
    public InputStream downFile(String filePath) {
        return returnBitMap(filePath);
    }

    /**
     * 通过网络地址获取文件InputStream
     *
     * @param path 地址
     * @return
     */
    public static InputStream returnBitMap(String path) {
        URL url = null;
        InputStream is = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();//利用HttpURLConnection对象,我们可以从网络中获取网页数据.
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();    //得到网络返回的输入流

        } catch (IOException e) {
            e.printStackTrace();
        }
        return is;
    }


}
